﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using NLBS.Provider;


namespace NLBS.Web.Modules
{
  public  class ReWriteUrlModule : IHttpModule
    {
        #region IHttpModule Members

        public void Dispose()
        {
           
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
            UrlReWriteProvider urlProvider = UrlReWriteProvider.Instance();

            string newPath = urlProvider.GetRewrittenUrl(HttpContext.Current);
            if (newPath != null)
            {
                string qs = null;
                int index = newPath.IndexOf('?');
                if (index >= 0)
                {
                    qs = (index < (newPath.Length - 1)) ? newPath.Substring(index + 1) : string.Empty;
                    newPath = newPath.Substring(0, index);
                }

                urlProvider.RewriteUrl(HttpContext.Current, newPath, qs);
            }
        }

        #endregion
    }
}
